package cn.dankal.flyaudio.personal;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import api.ShopUserServiceFactory;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.common.bus.LiveDataBusConstants;
import cn.dankal.basiclib.domain.HttpStatusCode;
import cn.dankal.basiclib.exception.LocalException;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.UserScoresEntity;
import cn.dankal.basiclib.pojo.UserScoresLogsEntity;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.ScoreRecordAdapter;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ScoreRecordActivity extends BaseActivity {

    private RecyclerView recyclerView;

    private ScoreRecordAdapter adapter;

    private TextView scoreDetailView;

    private List<String> arr  = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_score_record;
    }

    @Override
    protected void initComponents() {
        ImageView iv_wenhao = findViewById(R.id.iv_wenhao);
        scoreDetailView = findViewById(R.id.tv_score);
        adapter = new ScoreRecordAdapter();
        recyclerView = findViewById(R.id.swipe_target);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        iv_wenhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("type","3");
                ActivityUtils.startActivity(bundle,MyCardRuleActivity.class);
            }
        });
        scoreDetailView.setText(DKUserManager.getUserScores().getUsable());
        fetchUserScores();
        fetchRecord();

    }

    public void fetchUserScores(){
        ShopUserServiceFactory.userScores().subscribe(new Observer<BaseResponse<UserScoresEntity>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse<UserScoresEntity> userScoresEntityBaseResponse) {
                DKUserManager.updateUserInfoScores(userScoresEntityBaseResponse.getData());
                LiveEventBus.get(LiveDataBusConstants.REFRESH_USER_SCORES, String.class).post("");
                scoreDetailView.setText(DKUserManager.getUserScores().getUsable());
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof LocalException) {
                    LocalException exception = (LocalException) e;
                    //401 重新获取access token , 如果还返回412 就是refresh token 也失效了。需要重新登录
                    if (exception.getErrorCode() == HttpStatusCode.TOKEN_INVAILD||
                            exception.getErrorCode() == HttpStatusCode.UNAUTHORIZED) {
                        //view.showToast("登录信息失效");
                    } else {
                    }
                } else {
                    Log.e("SubscriberThrowable", e.getMessage());
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }
    private void fetchRecord(){
        ShopUserServiceFactory.userScoresLogs(1,10)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<UserScoresLogsEntity>>>(this) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<UserScoresLogsEntity>> baseListResponseBaseResponse) {

                        adapter.addDatas(baseListResponseBaseResponse.getData().getList(),true);
                    }
                });
    }

}
